package com.shengyin.ec.controller.goods;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shengyin.ec.exception.ControllerException;
import com.shengyin.ec.model.entity.GoodsType;
import com.shengyin.ec.service.goods.GoodsService;
import com.shengyin.ec.service.goods.GoodsTypeService;

@Controller
public class GoodsTypeController {

	@Autowired
	GoodsTypeService goodsTypeService;

	@Autowired
	GoodsService goodsService;

	@RequestMapping(value = "/toGoodsTypeCheck.do", method = RequestMethod.POST)
	public ModelMap toGoodsTypeCheck(@RequestParam("typeName") String typeName) {
		ModelMap modelMap = new ModelMap();
		boolean isExist = goodsTypeService.goodsTypeCheck(typeName);
		modelMap.addAttribute("isExist", isExist);
		return modelMap;
	}

	@RequestMapping(value = "/toGoodsTypeAdd.do")
	public String toGoodsTypeAdd(Model model) {
		ArrayList<GoodsType> typeList = (ArrayList<GoodsType>) goodsTypeService
				.getGoodsTypeList(0, 100);
		model.addAttribute("typeList", typeList);
		
		return "/admin/goods/goodsTypeAdd";
	}

	@RequestMapping(value = "/toGoodsType.do")
	public String toGoodsType(Model model, HttpServletRequest request)
			throws IOException {

		Long maxSize = goodsTypeService.getTotal();

		int offset = 0;
		try {
			offset = Integer.valueOf(request.getParameter("pager.offset"));
		} catch (Exception ex) {
			System.out.println("初始化页数.......");
		}
		ArrayList<GoodsType> typeList = (ArrayList<GoodsType>) goodsTypeService
				.getGoodsTypeList(offset, 10);// 默认10条才进行分页

		model.addAttribute("typeList", typeList);
		model.addAttribute("maxSize", maxSize);
		return "/admin/goods/goodsType";

	}

	@RequestMapping("/toModifyType.do")
	public String toModifyGoodsType(@RequestParam("id") int id, Model model) {

		GoodsType goodsType = goodsTypeService.getGoodsTypeById(id);
		model.addAttribute("goodsType", goodsType);
		return "/admin/goods/goodsTypeModify";

	}

	@RequestMapping("/goodsTypeAdd.do")
	public String createGoodsType(GoodsTypeForm goodsTypeForm) {
		try {
			GoodsType goodsType = new GoodsType();
			BeanUtils.copyProperties(goodsType, goodsTypeForm);
			if (goodsTypeForm.getParentId() != 0)
				goodsType.setChildNum(goodsType.getChildNum() + 1);
			goodsTypeService.createGoodsType(goodsType);
			return "redirect:/toGoodsType.do";

		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/deleteGoodsType.do")
	public String deleteGoodsType(@RequestParam("id") int id) {
		goodsTypeService.removeGoodsType(id);
		return "redirect:/toGoodsType.do";
	}

	@RequestMapping("/goodsTypeModify.do")
	public String modifyGoodsType(GoodsTypeForm goodsTypeForm) {
		try {
			GoodsType goodsType = new GoodsType();
			BeanUtils.copyProperties(goodsType, goodsTypeForm);
			goodsTypeService.updateGoodsType(goodsType);
			return "redirect:/toGoodsType.do";

		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping(value = "/getGoodsType.do", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap getGoodsType() {
		ModelMap modelMap = new ModelMap();
		List<GoodsType> list = goodsTypeService.getGoodsTypeList(0, 100);
		modelMap.addAttribute("list", list);
		return modelMap;
	}

	@RequestMapping(value = "/getGoodsTypeByParentId.do", method = RequestMethod.GET)
	@ResponseBody
	public ModelMap getGoodsTypeByParentName(
			@RequestParam("parentId") int parentId) {
		ModelMap modelMap = new ModelMap();
		List<GoodsType> list = goodsTypeService
				.getGoodsTypeByParentID(parentId);
		modelMap.addAttribute("list", list);
		return modelMap;
	}

	@RequestMapping(value="searchType.do")
	public String searchType(HttpServletRequest request,Model model){
		String keyword = request.getParameter("keyword");
		System.out.println("keyword===="+keyword);
		if(keyword == null) return "redirect:/toGoodsType.do";
		List<GoodsType> typeList = goodsTypeService.getGoodsTypeByKeyword(keyword,0,10);
		long maxSize=goodsTypeService.getGoodsTypeByKeyword(keyword).size();
		
		model.addAttribute("typeList",typeList);
		model.addAttribute("maxSize",maxSize);
		
		return "/admin/goods/goodsType";
	}
	@ExceptionHandler({ControllerException.class})  
	   public String exception(ControllerException e,HttpServletRequest request) {  
	       System.out.println(e.getMessage());  
	       request.setAttribute("error", e.getMessage());
	        return "/admin/error/error";  
	    }  
}
